//
//  PLRBroadcastRoomViewController.m
//  PLRLiveShow
//
//  Created by 赵小燕 on 2017/8/5.
//  Copyright © 2017年 xiaoyanzhao. All rights reserved.
//

#import "PLRBroadcastRoomViewController.h"
#import <LFLiveSession.h>

static NSString * const kHost = @"rtmp://192.168.1.200/hls/ios";

@interface PLRBroadcastRoomViewController ()

@property (nonatomic, strong) LFLiveSession *session;

@end

@implementation PLRBroadcastRoomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self requireDevicePermissionWithComplete:^(BOOL granted) {
        if (granted) {
            self.session.preView = self.view;
        }
    }];
        
    LFLiveStreamInfo *streamInfo = [LFLiveStreamInfo new];
    streamInfo.url = kHost;
    [self.session startLive:streamInfo];
}

// 请求摄像头和麦克风权限
- (void)requireDevicePermissionWithComplete:(void(^)(BOOL granted))complete {
    if ([self isCameraAvailable] && [self isMicrophoneAvailable]) {
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
            NSLog(@"授权失败");
        } else {
            NSLog(@"授权成功");
            complete(YES);
        }
        
        [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
            if (granted) {
                NSLog(@"授权成功");
                complete(YES);
            } else {
                NSLog(@"授权失败");
                complete(NO);
            }
        }];
        
    }
}

// 检测摄像头是否可用
- (BOOL)isCameraAvailable {
    return [UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera];
}

// 检测麦克风是否可用
- (BOOL)isMicrophoneAvailable {
    return [AVAudioSession sharedInstance].isInputAvailable;
}

// 检测是否存在前置摄像头
- (BOOL)isFrontCameraAvailable {
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (LFLiveSession*)session{
    if(!_session){
        LFLiveAudioConfiguration *audioConfiguration = [LFLiveAudioConfiguration new];
        audioConfiguration.numberOfChannels = 2;
        audioConfiguration.audioBitrate = LFLiveAudioBitRate_128Kbps;
        audioConfiguration.audioSampleRate = LFLiveAudioSampleRate_44100Hz;
        
        LFLiveVideoConfiguration *videoConfiguration = [LFLiveVideoConfiguration new];
        videoConfiguration.videoSize = CGSizeMake(1280, 720);
        videoConfiguration.videoBitRate = 800*1024;
        videoConfiguration.videoMaxBitRate = 1000*1024;
        videoConfiguration.videoMinBitRate = 500*1024;
        videoConfiguration.videoFrameRate = 15;
        videoConfiguration.videoMaxKeyframeInterval = 30;
        videoConfiguration.sessionPreset = LFCaptureSessionPreset720x1280;
        
        _session = [[LFLiveSession alloc] initWithAudioConfiguration:audioConfiguration videoConfiguration:videoConfiguration];
        _session.running = YES;
    }
    return _session;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.session stopLive];
}


@end
